package com.distributer.timer.zookeeper;

import org.apache.curator.framework.recipes.leader.LeaderLatchListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.distributer.timer.service.ScheduledTaskService;


@Component
public class ZookeeperClientListener implements LeaderLatchListener {

    private final static Logger log = LoggerFactory.getLogger(ZookeeperClientListener.class);

    @Autowired
    private ZookeeperConfig ZookeeperConfig;
    
    @Autowired
	private ScheduledTaskService scheduledTaskService;
    
    @Value("${clientId}")
	private String clientId;
    
    @Override
    public void isLeader() {
        log.info(clientId+" 当前服务已变为leader,将从事业务消费======>>>>");
        scheduledTaskService.initAllTask();
    }

    @Override
    public void notLeader() {
        log.info(clientId+" 当前服务已退出leader,不再从事消费业务=====>>>");
        scheduledTaskService.stopAllTask();
        scheduledTaskService.updateServiceToStop();
    }
}
